var tgd = tgd || {};
tgd.Utilities = tgd.Utilities || {};
tgd.Utilities.Countdown = tgd.Utilities.Countdown || {};

tgd.Utilities.Countdown.Controller = function () {
	this._timers = new Array();
	this._views = new Array();

	var self = this;
	var cereal = new tgd.Utilities.Countdown.Serializer(window.location.search);
	this._timers = cereal.getTimers();
	this._timers.sort(function(a, b) { return a.getEndDateTimeObject().getTime() - b.getEndDateTimeObject().getTime() });
	cereal = null;

	if (tgd.Utilities.Countdown.Controller._initialized === undefined) {
		/*
		 * t : tgd.Utilities.Countdown.Timer : timer to create a view for
		 * c : DOM element : the canvas of a tgd.Utilities.Countdown.Views.HTMLTimer of which the view for the current timer should be added before, if null then add to end of list
		 */
		function _spinView(t, c) {
			var allViewsDiv = document.getElementById("views");
			var viewCanvas = document.createElement("div");

			allViewsDiv.insertBefore(viewCanvas, c);
			viewCanvas.className = "timerContainer";

			var tempView = new tgd.Utilities.Countdown.Views.HTMLTimer(viewCanvas, t,
				function() {
					t.stop();
					allViewsDiv.removeChild(viewCanvas);
					tempView.setUpdateTitle(false);
					_resetUpdateTitleOnViews();
				});

			return tempView;
		}

		function _resetUpdateTitleOnViews() {
			var titleSet = false;
			document.title = "Countdown Timer";
			for (var i = 0; i < self._views.length; ++i) {
				self._views[i].setUpdateTitle(false);
				if (!titleSet && !self._views[i].isClosed()) {
					titleSet = true;
					self._views[i].setUpdateTitle(true);
				}
			}
		}
	}

	for (var i = 0; i < this._timers.length; ++i) {
		this._views[i] = _spinView(this._timers[i], null);
	}

	if (this._views.length > 0) {
		this._views[0].setUpdateTitle(true);
	}

	document.getElementById("txtTitle").focus();

	if (tgd.Utilities.Countdown.Controller._initialized === undefined) {

		tgd.Utilities.Countdown.Controller.prototype.addTimer = function() {
			var insertBeforeView = null;
			var newTimer = new tgd.Utilities.Countdown.Timer({
				Title: document.getElementById("txtTitle").value,
				EndDateTime: document.getElementById("txtEndDateTime").value
			});
			for (var i = this._timers.length; i >= 0; --i) {
				if (i == 0 || this._timers[i - 1].getEndDateTimeObject() < newTimer.getEndDateTimeObject()) {
					this._timers[i] = newTimer;
					this._views[i] = _spinView(newTimer, (insertBeforeView != null ? insertBeforeView.getCanvas() : null));
					break;
				} else {
					this._timers[i] = this._timers[i - 1];
					this._views[i] = this._views[i - 1];
					if (!this._views[i].isClosed()) {
						insertBeforeView = this._views[i];
					}
				}
			}

			document.getElementById("txtTitle").value = "";
			document.getElementById("txtEndDateTime").value = "";
			document.getElementById("txtTitle").focus();

			_resetUpdateTitleOnViews();
		}

		tgd.Utilities.Countdown.Controller.prototype.serialize = function() {
			cereal = new tgd.Utilities.Countdown.Serializer(this._views);
			window.location.search = cereal.getSerialization();
		}

		tgd.Utilities.Countdown.Controller._initialized = true;
	}
}